﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using Microsoft.AspNetCore.SignalR;
using YXChatApiService.Application.AccountServices.Dtos;
using YXChatApiService.Application.BuddyServices;
using YXChatApiService.Application.BuddyServices.Dtos;
using YXChatApiService.Core.AccountEntity.DomainManager;
using YXChatApiService.Core.BuddyEntity.DomainManager;
using YXChatApiService.Filters;
using YXChatApiService.HubCommon;

namespace YXChatApiService.Controllers
{
    public class BuddyController : ApiControllerBase
    {
        private readonly IBuddyAppService _buddyAppService;
        private readonly IMapper _mapper;
        private readonly IAccountManager _accountManager;
        private readonly IHubContext<ChatHub, IChatService> _chatHub;
        public BuddyController(IBuddyAppService buddyAppService, IHubContext<ChatHub, IChatService> chatHub, IAccountManager accountManager, IMapper mapper)
        {
            _buddyAppService = buddyAppService;
            _chatHub = chatHub;
            _accountManager = accountManager;
            _mapper = mapper;
        }
        /// <summary>
        /// 查询用户信息以及关系
        /// </summary>
        /// <param name="selfNumber"></param>
        /// <param name="buddyNumber"></param>
        /// <returns></returns>
        [HttpGet("{selfNumber}/{buddyNumber}")]
        [ResultDataFilter]
        public async Task<BuddyAccountOutput> SearchBuddyInfo(string selfNumber,string buddyNumber)
        {
            return await _buddyAppService.SearchBuddyInfo(selfNumber,buddyNumber);
        }
        /// <summary>
        /// 添加好友关系或修改
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [ResultDataFilter]
        public async Task<bool> CreateOrUpdateBuddy(BuddyCreateOrUpdateInput input)
        {
            var result= await _buddyAppService.CreateOrUpdateBuddy(input);
            // 要通知的好友的客户端
            var buddyClient = HubClients.AccountClients.FirstOrDefault(n => n.AccountNumber == input.AddeeAccountNumber);
            // 自己的账号信息通知给好友
            var buddyAccount = _accountManager.GetAccount(input.AdderAccountNumber);
            var accountDto = _mapper.Map<AccountDto>(buddyAccount);
            if (input.Id.HasValue)
            {
                if (buddyClient != null) await _chatHub.Clients.Client(buddyClient.ConnectionId).AddBuddyNotify(accountDto);
            }
            return result;
        }
        [HttpGet("{selfNumber}")]
        [ResultDataFilter]
        public async Task<List<BuddyAccountOutput>> GetNewBuddys(string selfNumber)
        {
            return await _buddyAppService.GetNewBuddyList(selfNumber);
        }
        [HttpGet("{selfNumber}")]
        [ResultDataFilter]
        public async Task<List<BuddyAccountOutput>> GetBuddys(string selfNumber)
        {
            return await _buddyAppService.GetBuddyAll(selfNumber);
        }
    }
}
